package com.crane.oss.school.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.school.bean.request.QueryEquipmentReq;
import com.crane.oss.school.bean.request.equipment.AddEquipmentReq;
import com.crane.oss.school.bean.request.equipment.UpdateEquipmentReq;
import com.crane.oss.school.bean.resp.EquipmentResp;
import com.crane.oss.school.dao.EquipmentDao;
import com.crane.oss.school.dao.StudentEquipmentDao;
import com.crane.oss.school.model.Equipment;
import com.crane.oss.school.model.StudentEquipment;
import com.crane.oss.school.service.EquipmentService;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2023年01月17日 23时03分27秒
 */
@Service
@Slf4j
public class EquipmentServiceImpl implements EquipmentService {

	@Resource
	protected EquipmentDao equipmentDao;
	
	@Resource
	protected StudentEquipmentDao studentEquipmentDao;

	@Override
	public EquipmentResp queryEquipment(Long id) {
		log.info("EquipmentBOImpl queryEquipment id={}", id);
		Equipment equipment = equipmentDao.selectById(id);
		if (null == equipment) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "equipment");
		}
		EquipmentResp equipmentResp = new EquipmentResp();
		BeanUtil.copyProperties(equipment, equipmentResp);
		return equipmentResp;
	}

	@Override
	public IPage<EquipmentResp> queryList(QueryEquipmentReq queryRequest) {
		log.info("EquipmentBOImpl queryList queryRequest={}", queryRequest);
		IPage<EquipmentResp> result = new Page<>();
		result.setCurrent(queryRequest.getPageNo());
		result.setPages(queryRequest.getPageSize());

		LambdaQueryWrapper<Equipment> queryEquipmentWrapper = new LambdaQueryWrapper<>();
		IPage<Equipment> page = equipmentDao
				.selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()), queryEquipmentWrapper);
		return page.convert(u -> {
			EquipmentResp v = new EquipmentResp();
			BeanUtils.copyProperties(u, v);
			return v;
		});
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int save(AddEquipmentReq addRequest) {
		log.info("EquipmentServiceImpl save request={}", addRequest);
		//根据学校ID、校区ID、imei查询设备是否已存在
		Equipment queryEquipment = equipmentDao.selectOne(Wrappers.lambdaQuery(Equipment.class)
				.eq(Equipment::getPartyId, addRequest.getPartyId())
				.eq(Equipment::getSchoolId, addRequest.getSchoolId())
				.eq(Equipment::getImei, addRequest.getImei()));
		if (queryEquipment != null) {
			//根据学生ID、设备ID查询是否已绑定
			StudentEquipment queryStudentEquipment = studentEquipmentDao.selectOne(Wrappers.lambdaQuery(StudentEquipment.class)
					.eq(StudentEquipment::getStudentId, addRequest.getStudentId())
					.eq(StudentEquipment::getEquipmentId, queryEquipment.getEquipmentId()));
			if (null != queryStudentEquipment) {
				throw new AppException(BasicErrorCode.DEFINED_MSG, "已绑定");
			}
		}
		
		Equipment newEquipment = new Equipment();
		BeanUtil.copyProperties(addRequest, newEquipment);
		
		Date date = new Date();
		
		int i = 0;
		try {
			Long equipmentId = null;
			if (queryEquipment == null) {
				newEquipment.setCreateTime(date);
				equipmentDao.insert(newEquipment);
				equipmentId = newEquipment.getEquipmentId();
			} else {
				equipmentId = queryEquipment.getEquipmentId();
			}
			
			StudentEquipment newStudentEquipment = new StudentEquipment();
			newStudentEquipment.setStudentId(addRequest.getStudentId());
			newStudentEquipment.setEquipmentId(equipmentId);
			newStudentEquipment.setName(addRequest.getName());
			newStudentEquipment.setBindType(addRequest.getBindType());
			
			Integer bindStatus = 1;
			Integer useStatus = 1;
			if (addRequest.getBindType()==2) {
				bindStatus = 3;
				useStatus = 3;
			}
			
			newStudentEquipment.setBindStatus(bindStatus);
			newStudentEquipment.setUseStatus(useStatus);
			newStudentEquipment.setBindTime(date);
			newStudentEquipment.setCreateTime(date);
			
			i = studentEquipmentDao.insert(newStudentEquipment);
		} catch (Exception e) {
			log.info("EquipmentServiceImpl save e={}", e);
			throw new SysException();
		}
		return i;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int update(UpdateEquipmentReq updateRequest) {
		log.info("EquipmentServiceImpl update request={}", updateRequest);
		Equipment equipment = new Equipment();
		BeanUtil.copyProperties(updateRequest, equipment);
		int i = 0;
		try {
			i = equipmentDao.updateById(equipment);
		} catch (Exception e) {
			log.info("EquipmentServiceImpl add e={}", e);
			throw new SysException();
		}
		return i;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int delete(Long id) {
		log.info("EquipmentServiceImpl delete id={}", id);
		return equipmentDao.deleteById(id);
	}

}
